﻿<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" MasterPageFile="~/administration/list_private.master"%>
<%@ MasterType VirtualPath="~/administration/list_private.master" %>
<script runat="server">
	private System.Resources.ResourceManager rm;
	
	string inst = "";
	string celm = "";
	string author = "";
	void Page_Load(object o, EventArgs e) {
		if(Application["RM_ADM"]!=null)
    			rm = (System.Resources.ResourceManager) Application["RM_ADM"];
		if(Request.Params["inst"]!=null)
			inst = Request.Params["inst"].ToString();
		if(Request.Params["celm"]!=null)
			celm = Request.Params["celm"].ToString();
			
		if(this.Master is ACM.cmsarea08.Administration.Core.ListerPrivate) {
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.Visible = false;
		}
		
		author = ACM.cmsarea08.Users.CurrentUser.IdUtente;
	}


</script>
<asp:Content ID="PageBody" Runat="server" ContentPlaceHolderID="MasterBody">
<script type="text/javascript" src="/template/plugins/mootools-1.js"></script>
<script type="text/javascript" src="SushiFileManager_uploadObject.js"></script>
<style type="text/css">
@import url("fmanager.css");
</style>
</head>
  <h2>File Manager</h2>
  <div id="filemanager">
  <div id="viewselect"><ul><li><%= rm.GetString("view_files") %>: </li><li><a href="javascript:void();"><%= rm.GetString("list") %></a></li><li><a href="javascript:void();"><%= rm.GetString("grid") %></a></li></ul></div>
    <div id="uploadzone">
    	<div id="uploadbutton"><%= rm.GetString("button") %></div>
        <div id="uploadcmd"><%= rm.GetString("upload_file") %></div>
    </div>
    <div id="filezone">
      <table cellpadding="0" cellspacing="0" border="0" align="center" id="tablefiles">
      <thead>
        <tr>
          <td width="30%"><%= rm.GetString("name") %></td>
          <td width="17%"><%= rm.GetString("group") %></td>          
          <td width="13%"><%= rm.GetString("uploaded_by") %></td>
          <td width="5%"><%= rm.GetString("size") %></td>
          <td width="15%"><%= rm.GetString("data_ins") %></td>
          <td width="7%">&nbsp;</td>
        </tr>
        </thead>
        <tbody>
        <tr>
          <td>Cicciolina va in campagna.jpg</td>
          <td>Francesco</td>          
          <td>Francesco</td>
          <td>55,5 Kb</td>
          <td>2008-02-12</td>
          <td>X</td>
          </tr></tbody>
      </table>
    </div>
</div>

</form>

<script language="javascript">
//qui ninizializzo l'oggeto..

var TableLayout ="tablefiles";
var CurrentIstance = "105"; 
var RelatedObject  = "2";
var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp=false;
  }
 }
@else
 xmlhttp=false
 @end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

function GetNodeValue(xmlnode) {
	if(xmlnode.childNodes.length>0)
		return xmlnode.firstChild.nodeValue;
	else
		return "";
}



function LeggiFileTable(idistanza) {
	//per leggere i files.
	var myXhr = new XHR({async: true, method: 'get', onSuccess: function(text,xml) {
			var cXml ;
			if(text==null) { /*alert('sno nullo');*/return;}
			//qui ci va l'if di ie:
			CleanTable();
			if (window.ActiveXObject)
			{
				var xmlObj=new ActiveXObject("Microsoft.XMLDOM");
				xmlObj.async="false";
				xmlObj.loadXML(text);
				if(xmlObj.documentElement == null) {	/*alert('sono nullo');*/ return; }
				cXml = xmlObj.documentElement;		
			} else {
				cXml = xml.documentElement;
			}
			if(cXml == null) { /*alert('cXml nullo....');*/ return; }
			var filez = cXml.getElementsByTagName('Table');
			for (i=0;i<filez.length;i++)
			{	
				var _id ="";
				var _pname ="";
				var _srcname="";
				var _related="";
				var _author="";						
				var _filesize="";						
				var _data_mod="";						
				var _instanceid="";												
				var _gname="";				
				for(var c = 0 ; c<filez[i].childNodes.length; c++) {
					if(  filez[i].childNodes[c].nodeName != '#text') {
						//getNoodeValue:
						if( filez[i].childNodes[c].nodeName.toString().toLowerCase()=='id' )
							_id = GetNodeValue( filez[i].childNodes[c] );
						if( filez[i].childNodes[c].nodeName.toString().toLowerCase()=='pname' )
							_pname = GetNodeValue( filez[i].childNodes[c] );
						if( filez[i].childNodes[c].nodeName.toString().toLowerCase()=='srcname' )
							_srcname = GetNodeValue( filez[i].childNodes[c] );
						if( filez[i].childNodes[c].nodeName.toString().toLowerCase()=='related' )
							_related = GetNodeValue( filez[i].childNodes[c] );
						if( filez[i].childNodes[c].nodeName.toString().toLowerCase()=='authorname' ) //author
							_author = GetNodeValue( filez[i].childNodes[c] );
						if( filez[i].childNodes[c].nodeName.toString().toLowerCase()=='filesize' )
							_filesize = GetNodeValue( filez[i].childNodes[c] );
						if( filez[i].childNodes[c].nodeName.toString().toLowerCase()=='data_mod' )
							_data_mod = GetNodeValue( filez[i].childNodes[c] );
						if( filez[i].childNodes[c].nodeName.toString().toLowerCase()=='instanceid' )
							_instanceid = GetNodeValue( filez[i].childNodes[c] );
						if( filez[i].childNodes[c].nodeName.toString().toLowerCase()=='group_name' ) 
							_gname = GetNodeValue( filez[i].childNodes[c] ) ;
					}
				}			
				//create table row.		
				CreateTableRow(	_id, _pname, _srcname, _related, _author, _filesize	, _data_mod	, _instanceid, _gname);			
			}			
			
		}
	});
	myXhr.send('xml_server.asmx/getFiles', 'idinstance=' + idistanza);			
}

function TimeStampToDate(xmlDate)
{
    var dt = new Date();
    var dtS = xmlDate.slice(xmlDate.indexOf('T')+1, xmlDate.indexOf('.'))
    var TimeArray = dtS.split(":");
    dt.setUTCHours(TimeArray[0],TimeArray[1],TimeArray[2]);
    dtS = xmlDate.slice(0, xmlDate.indexOf('T'))
    TimeArray = dtS.split("-");
    dt.setUTCFullYear(TimeArray[0],TimeArray[1],TimeArray[2]);
    return dt;
}

function CreateTableRow(id,pname,srcname,related,author,filesize,datamod,instanceid, gname) {
	var dt = TimeStampToDate(datamod);
	var kbyte = parseInt(filesize);
	var bytetipe = "";
	if(kbyte>1048576) {
		kbyte = ((kbyte/1024)/1024).toFixed(2);
		bytetipe = "Mb";
	} else if( kbyte>1024 )  {
		kbyte = (kbyte/1024).toFixed(2) ;
		bytetipe = "kb" ;
	} else {
		bytetipe = "byte" ;
	}
	var tobj = $(TableLayout);
	var tbody = tobj.getElements('tbody');
	if(tbody==null) { /*alert('nulla table'); */return; }
	var tr = new Element('tr');
	tr.setProperty('id','file_' + id);
	var td1 = new Element('td');
	var td2 = new Element('td');
	var td3 = new Element('td');
	var td4 = new Element('td');
	var td5 = new Element('td');
	var td6 = new Element('td');
	
	
	td6.addClass('EditBtms');
	var editbtm = new Element('a').setHTML('<img src=\'ico/edit.gif\' alt=\'Edit\' width=\'18\' />');
	editbtm.setProperty('title','Edit');
	var hiddenValue = new Element('input').setProperty('type','text');
	hiddenValue.setProperty('id','Epname_' + id);
	hiddenValue.setProperty('value',pname);	
	hiddenValue.setStyle('display','none');		
	var spanValue = new Element('a').setHTML(pname);	
	spanValue.setProperty('id','Rpname_' + id);
	spanValue.setProperty('rel',id);	
	spanValue.addClass('downbtm');
	spanValue.addEvent('click', function(e) {
		//alert('sono qui... ' + e);
		evt = new Event(e);evt.stop();
		var elemento = new Element('iframe');
		var ido = this.getProperty('rel') ;
		elemento.setProperty('src',"fdown.aspx?ifile=" + ido );
		elemento.setStyle('display','none');
		elemento.injectAfter(this);
		//alert('...');
	});
	spanValue.addEvent('mouseenter',function(e) {
		evt = new Event(e);evt.stop();
		var elemento = new Element('div');
		elemento.setProperty('id','ThumbPreview');
		elemento.setHTML(this.getProperty('rel')); 
		elemento.setStyle('display','block');
		elemento.setStyle('position','absolute');	
		//loader di thumbs e di icone...
		
		var thisx = this.getPosition().x + this.getSize().scrollSize.x - (this.getSize().scrollSize.x/3);	
		var thisy = this.getPosition().y + this.getSize().scrollSize.y - (this.getSize().scrollSize.y/2) ;			
		elemento.setStyles(
		 {
		 	'position':'absolute',
			'width':'50px',
			'height':'50px',
			'opacity':'0',			
			'background-color':'#FFFFFF',
			'border':'1px solid #666666',
			'top':thisy + 'px',
			'left':thisx + 'px'
		 } );		
		 elemento.injectAfter(this);
		 elemento.effect('opacity', {duration: 250, transition: Fx.Transitions.linear}).start(0,0.8);

	});
	spanValue.addEvent('mouseleave',function(e) {
		evt = new Event(e);evt.stop();
		var elemento = $('ThumbPreview');
		if(elemento!=null) {
			elemento.remove();
		}
	});


	var gnameValue = new Element('input').setProperty('type','text');
	gnameValue.setProperty('id','Egname_' + id);
	gnameValue.setProperty('value',gname);	
	gnameValue.setStyle('display','none');		
	var gspanValue = new Element('span').setHTML(gname);	
	gspanValue.setProperty('id','Rgname_' + id);
	
//	td2.setHTML(pname);	   // <--- al click avviare download
	td1.adopt(hiddenValue);
	td1.adopt(spanValue);
	
//	td4.setHTML(gname);				
	td2.adopt(gnameValue);
	td2.adopt(gspanValue);
//	
	td3.setHTML(author);   // nome utente associato.
	td4.setHTML(kbyte + ' '+bytetipe);	
	td5.setHTML(dt.toLocaleString());									
//	td8.setHTML('&nbsp;');									
	td6.adopt(editbtm);
	editbtm.addEvent('click', function(e) {
		evt = new Event(e);evt.stop();
		EditMe(this);
	});
	tr.adopt(td1);
	tr.adopt(td2);
	tr.adopt(td3);	
	tr.adopt(td4);
	tr.adopt(td5);
	tr.adopt(td6);
	tbody[0].adopt(tr);
}

function EditMe(src) {
//		alert('vuoi entrare in modo Edit...presumo');
		var tdP = src.getParent();
		if(tdP == null) { /*alert('errore parent');*/ return; }
		var trP = tdP.getParent();		
		if(trP == null) { /*alert('errore Row parent');*/ return; }		
		var id_item = trP.getProperty('id');
		id_item = id_item.replace('file_','');
		//trova elementi: 
		var spanP = $('Rpname_' + id_item);
		var inputP = $('Epname_' + id_item);		
		if(spanP!=null && inputP!=null) {
			spanP.setStyle('display','none');
			inputP.setStyle('display','block');		
		}
		var spanG = $('Rgname_' + id_item);
		var inputG = $('Egname_' + id_item);		
		if(spanG!=null && inputG!=null) {
			spanG.setStyle('display','none');
			inputG.setStyle('display','block');		
		}		
		//trova l'ultimo indice di ".";
		var lastPoint = inputP.value.lastIndexOf('.');
		inputP.focus();	
		if( inputP.createTextRange) {
			var rang = inputP.createTextRange();
			rang.collapse(true);
			rang.moveStart("character",0);
			rang.moveEnd("character",lastPoint);
			rang.select();
		} else if (inputP.setSelectionRange) {
			inputP.setSelectionRange(0,lastPoint);
		}
		
		tdP.empty();
		var annBtm = new Element('a').setHTML('<img src=\'ico/reset.gif\' alt=\'Reset\' width=\'18\' />');	
		annBtm.setProperty('title','Reset');			
		var delBtm = new Element('a').setHTML('<img src=\'ico/delete.gif\' alt=\'Delete\' width=\'18\' />');
		delBtm.setProperty('title','Delete');		
		var savBtm = new Element('a').setHTML('<img src=\'ico/save.gif\' alt=\'Save\' width=\'18\' />');
		savBtm.setProperty('title','Save');		
		savBtm.addEvent('click', function(e) { evt = new Event(e);evt.stop(); SaveMe(this);});
		delBtm.addEvent('click', function(e) { evt = new Event(e);evt.stop(); DeleteMe(this);});
		annBtm.addEvent('click', function(e) { evt = new Event(e);evt.stop(); AnnullaMe(this);});		
		tdP.adopt(savBtm);
		tdP.adopt(annBtm);		
		tdP.adopt(delBtm);		
}
function AnnullaMe(src) {
	LeggiFileTable(CurrentIstance);
}
function DeleteMe(src) {
	var tdP = src.getParent();
	var trP = tdP.getParent();
	var idObj = trP.getProperty('id');
	idObj = idObj.replace('file_','');
	var myXhr = new XHR({async: true, method: 'get', onSuccess: function(text,xml) {
					if(text==null) {/*alert('no delete');*/ return; }
					var cXml;
					if (window.ActiveXObject)
					{
						var xmlObj=new ActiveXObject("Microsoft.XMLDOM");
						xmlObj.async="false";
						xmlObj.loadXML(text);
						if(xmlObj.documentElement == null) {/*	alert('sono nullo');*/ return; }
						cXml = xmlObj.documentElement;		
					} else {
						cXml = xml.documentElement;
					}					
					var filez = cXml.childNodes.length;
					if( cXml.childNodes.length>0)  {
						filez = cXml.childNodes[0];
					}
					if(filez!=null) {
						var reso = filez.nodeValue;
						if(reso == null) { /*alert('no delete return'); */return; }			
						if(reso.toString().toUpperCase() =="OK") {
							LeggiFileTable(CurrentIstance);
						} /*else {
							alert('no delete error code: ' + reso);
						}*/
					} else {
						/*alert('no return delete');*/
						return;
					}
			}
	});
	//DeleteFile(string id)
	myXhr.send('xml_server.asmx/DeleteFile', 'id='+idObj);			
	LeggiFileTable(CurrentIstance);
}
function SaveMe(src) {
	var tdP = src.getParent();
	var trP = tdP.getParent();
	var newName = "";	
	var newGroup = "";		
	var id_item = trP.getProperty('id');
	id_item = id_item.replace('file_','');
	//trova elementi: 
	var spanP = $('Rpname_' + id_item);
	var inputP = $('Epname_' + id_item);		
	if(spanP!=null && inputP!=null) {
		newName = inputP.getProperty('value');
		spanP.setStyle('display','block');
		inputP.setStyle('display','none');		
	}
	var spanG = $('Rgname_' + id_item);
	var inputG = $('Egname_' + id_item);		
	if(spanG!=null && inputG!=null) {
		newGroup = inputG.getProperty('value');
		spanG.setStyle('display','block');
		inputG.setStyle('display','none');		
	}	
	
	var myXhr = new XHR({async: true, method: 'get', onSuccess: function(text,xml) {
					if(text==null) {/*alert('no upload');*/ return; }
					var cXml;
					if (window.ActiveXObject)
					{
						var xmlObj=new ActiveXObject("Microsoft.XMLDOM");
						xmlObj.async="false";
						xmlObj.loadXML(text);
						if(xmlObj.documentElement == null) {	/*alert('sono nullo');*/ return; }
						cXml = xmlObj.documentElement;		
					} else {
						cXml = xml.documentElement;
					}					
					var filez = cXml.childNodes.length;
					if( cXml.childNodes.length>0)  {
						filez = cXml.childNodes[0];
					}
					if(filez!=null) {
						var reso = filez.nodeValue;
						if(reso == null) { /*alert('no upload return'); */return; }			
						if(reso.toString().toUpperCase() =="OK") {
							LeggiFileTable(CurrentIstance);
						} /*else {
							alert('no upload error code: ' + reso);
						}*/
					} else {
						/*alert('no return upload');*/
						return;
					}

			}
	});
	//EditFile(string id, string newName, string newGroup)
	var parametri = Object.toQueryString({
			id: 		id_item, 
			newName: 	newName,
			newGroup: 	newGroup
			});
	myXhr.send('xml_server.asmx/EditFile', parametri);			
	LeggiFileTable(CurrentIstance);
}

function CleanTable() {
	var tobj = $(TableLayout);
	var tbody = tobj.getElements('tbody');
	if(tbody==null) { /*alert('nulla table'); */return; }
	try {
		var iR = tbody[0].rows.length-1;
		while( tbody[0].rows.length>0) {
			tbody[0].deleteRow(iR);
			iR--;
		}
	} catch (err) {
		/*alert('err: ' + err);*/
	}
}

var mioUPD = new SushiFileUpload('uploadbutton','mioUPD');
mioUPD.addEvent('saving', function(guid,value,groupname,filename,who){
		var myXhr = new XHR({async: true, method: 'get', onSuccess: function(text,xml) {
					if(text==null) {/*alert('no save'); */return; }
					var cXml;
					if (window.ActiveXObject)
					{
						var xmlObj=new ActiveXObject("Microsoft.XMLDOM");
						xmlObj.async="false";
						xmlObj.loadXML(text);
						if(xmlObj.documentElement == null) {	/*alert('sono nullo'); */return; }
						cXml = xmlObj.documentElement;		
					} else {
						cXml = xml.documentElement;
					}					
					var filez = cXml.childNodes.length;
					if( cXml.childNodes.length>0)  {
						filez = cXml.childNodes[0];
					}
					if(filez!=null) {
						var reso = filez.nodeValue;
						if(reso == null) { /*alert('no save return'); */return; }			
						if(reso.toString().toUpperCase() =="OK") {
							LeggiFileTable(CurrentIstance);
						} else {
							alert('no save error code: ' + reso);
						}
					} else {
						/*alert('no return value');*/
						return;
					}
					mioUPD.clearUpload(guid);
				}
				
		});
		//SaveFile(string guid, string groupname, string instance,string pfilename,string related)
		var parametri = Object.toQueryString({
				guid: 		guid, 
				groupname: 	groupname,
				instance: 	CurrentIstance,
				pfilename: 	filename,
				related: 	RelatedObject,
				author:     '<%= author %>'
				});
		myXhr.send('xml_server.asmx/SaveFile', parametri);	
		
    });
	
window.addEvent('domready', function() {
	
	//evento di lettura file in tabella:
	LeggiFileTable(CurrentIstance);




});
</script>
</asp:content>